package com.wpre.blogsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wpre.blogsystem.common.pojo.dataobject.BlogInfo;
import com.wpre.blogsystem.common.pojo.dataobject.UserInfo;
import com.wpre.blogsystem.common.pojo.request.UserLoginParam;
import com.wpre.blogsystem.common.pojo.response.UserInfoResponse;
import com.wpre.blogsystem.common.pojo.response.UserLoginResponse;
import com.wpre.blogsystem.common.utils.BeanTransfer;
import com.wpre.blogsystem.common.utils.JwtUtil;
import com.wpre.blogsystem.common.utils.SecurityUtil;
import com.wpre.blogsystem.exception.BlogException;
import com.wpre.blogsystem.mapper.BlogInfoMapper;
import com.wpre.blogsystem.mapper.UserInfoMapper;
import com.wpre.blogsystem.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-12-06
 * Time: 17:07
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource(name = "userInfoMapper")
    private UserInfoMapper userInfoMapper;
    @Resource(name = "blogInfoMapper")
    private BlogInfoMapper blogInfoMapper;

    @Override
    public UserLoginResponse login(UserLoginParam userParam) {
        UserInfo userInfo = userInfoMapper.selectOne(new LambdaQueryWrapper<UserInfo>()
                .eq(UserInfo::getUserName, userParam.getUserName())//
                .eq(UserInfo::getDeleteFlag, 0));//从数据库中查询一个用户
        if (userInfo == null) {
            throw new BlogException("用户不存在");
        }
        //校验密码是否正确
        if (!SecurityUtil.verify(userParam.getPassword(), userInfo.getPassword())) {
            //密码错误
            throw new BlogException("密码错误");
        }
        UserLoginResponse response = new UserLoginResponse();
        response.setUserId(userInfo.getId());
        //载荷
        Map<String, Object> map = new HashMap<>();
        map.put("name", userInfo.getUserName());
        map.put("id", userInfo.getId());

        response.setToken(JwtUtil.getToken(map));

        return response;
    }

    @Override
    public boolean register(UserLoginParam userParam) {
        UserInfo userInfo = new UserInfo();
        userInfo.setGithubUrl("https://blog.csdn.net/QUIXOTIC_?spm=1000.2115.3001.5343");
        userInfo.setDeleteFlag(0);
        userInfo.setUserName(userParam.getUserName());
        String inputPassword = userParam.getPassword();
        String finalPassword = SecurityUtil.encrypt(inputPassword);//加密
        userInfo.setPassword(finalPassword);//设置密码(经过了加密)
        try {
            int insert = userInfoMapper.insert(userInfo);
            if (insert == 1) {
                return true;
            }
        } catch (BlogException e) {
            throw new BlogException("注册失败");
        }
        return false;
    }

    @Override
    public UserInfoResponse getUserInfoById(Integer userId) {
        UserInfo userInfo = selectUserInfoById(userId);
        return BeanTransfer.trans(userInfo);//UserInfo转换成UserInfoResponse
    }

    @Override
    public UserInfoResponse getAuthorInfo(Integer blogId) {
        //1.根据博客id获取作者id
        BlogInfo blogInfo = blogInfoMapper.selectOne(new LambdaQueryWrapper<BlogInfo>()
                .eq(BlogInfo::getId, blogId)
                .eq(BlogInfo::getDeleteFlag, 0));

        if (blogInfo == null) {
            throw new BlogException("博客不存在");
        }
        //2.根据作者id获取作者详情
        UserInfo userInfo = selectUserInfoById(blogInfo.getUserId());
        return BeanTransfer.trans(userInfo);
    }


    //通过UserId查询用户信息
    public UserInfo selectUserInfoById(Integer userId) {
        return userInfoMapper.selectOne(new LambdaQueryWrapper<UserInfo>()
                .eq(UserInfo::getId, userId).eq(UserInfo::getDeleteFlag, 0));
    }
}
